﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace uz.control.CustomControl
{
    public class UzTextBox : TextBox
    {
        public UzTreeNode Node { get; set; }
        public UzTextBox(UzTreeNode node)
        {
            this.Node = node;
        }

        public AutoComplateWindows AutoComplateWindows { get; set; }

        protected override bool IsInputKey(Keys keyData)
        {
            if (keyData == (Keys.Tab | Keys.Shift) || keyData == Keys.Tab)
            {
                return true;
            }
            else
            {
                return base.IsInputKey(keyData);
            }
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            if (AutoComplateWindows != null)
            {
                AutoComplateWindows.ProcessKeyUp(e);
            }
            base.OnKeyUp(e);
        }

        protected override void OnParentChanged(EventArgs e)
        {
            base.OnParentChanged(e);
            if (AutoComplateWindows != null)
            {
                AutoComplateWindows.HideAutoComplate();
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (AutoComplateWindows != null && AutoComplateWindows.IsAutoComplate == true)
            {
                return AutoComplateWindows.ProcessCmdKey(keyData);
            }
            else
            {
                return base.ProcessCmdKey(ref msg, keyData);
            }
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            int s = this.SelectionStart;
            int l = this.SelectionLength;

            if (e.KeyData == (Keys.Tab | Keys.Shift))
            {
                string str = this.Text;
                int count = 0;
                for (int i = s - 1; i >= 0; i--)
                {
                    if (str.Substring(i, 1 ) == " ")
                    {
                        count++; 
                    }
                    else
                    {
                        break;      
                    }

                    if (count >= 4)
                    {
                        break;
                    }
                }

                str = str.Remove(s - count, count);

                this.Text = str;
                this.SelectionStart = s - count;
                this.SelectionLength = l;
            }
            else if (e.KeyData == Keys.Tab)
            {
                this.Text = this.Text.Insert(this.SelectionStart, "    ");
                this.SelectionStart = s + 4;
                this.SelectionLength = l;
            }
            else if (e.KeyCode == Keys.Up)
            {
                UzTreeNode node = Node.GetPrevBrotherNode();
                this.Node.IsEdit = false;
                //this.Node.UzTreeView.MovePrev();
            }
            else if (e.KeyCode == Keys.Down)
            {
                this.Node.IsEdit = false;
                this.Node.UzTreeView.MoveNext();
            }
            else if (e.KeyCode == Keys.F2)
            {
                this.Node.IsEdit = false;
            }
            else
            {
                base.OnKeyDown(e);
            }
        }
    }
}
